﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SweeperPrototype.Business;
using SweeperPrototype.Business.Fields;
using SweeperPrototype.Business.Keys;
using SweeperPrototype.Business.Plumbing;

namespace SweeperPrototype.Objects
{

    // ok so we want to provide a 'live aggregate'
    public class ExposureMapProvider : NotifyList<ExposureMappingSubscription>
    {
        private readonly Dictionary<ExposureMappingSubscription.UniqueKey, ExposureMappingSubscription> m_Mappings;
        private readonly Dictionary<ExposureRIC, NotifyList<ExposureMappingSubscription>> m_LookupByExposure;
        private readonly Dictionary<EffectiveRIC, NotifyList<ExposureMappingSubscription>> m_LookupByEffective;

        private readonly IExposureMappingSource m_Source;

        public ExposureMapProvider( IExposureMappingSource source )
        {
            m_Source = source;

            m_Mappings = new Dictionary<ExposureMappingSubscription.UniqueKey, ExposureMappingSubscription>();
            m_LookupByExposure = new Dictionary<ExposureRIC, NotifyList<ExposureMappingSubscription>>();
            m_LookupByEffective = new Dictionary<EffectiveRIC, NotifyList<ExposureMappingSubscription>>();
        }

        public INotifyList<ExposureMappingSubscription> Subscribe(ExposureRIC ric)
        {
            NotifyList<ExposureMappingSubscription> list;
            if( m_LookupByExposure.TryGetValue(ric, out list) == false )
            {
                list = new NotifyList<ExposureMappingSubscription>();
                m_LookupByExposure.Add( ric, list );

                // TODO: send underlying subscription out.

                //ExposureMappingSubscription map = new ExposureMappingSubscription(
                //    ric, new EffectiveRIC(ric.Value)
                //    );

                List<ExposureMappingSubscription> data = m_Source.FetchExposureMappings(ric);
                //NotifyList<ExposureMappingSubscription> effective_list = new NotifyList<ExposureMappingSubscription>();

                foreach( ExposureMappingSubscription map in data )
                {
                    //map.Update(new ExposureMappingSubscription.ExposureMappingUpdate(new Multiplier(1), new Timestamp(DateTime.Now)));
                    //list.Add(map);
                    
                    list.Add( map );

                    //m_LookupByEffective.Add(map.EffectiveRIC, effective_list);

                    Add(map);
                    
                }
            }
            return list;
        }

        public bool TryGet( ExposureRIC exposure, out NotifyList<ExposureMappingSubscription> map_provider )
        {
            return m_LookupByExposure.TryGetValue(exposure, out map_provider);
        }

        public bool TryGet( EffectiveRIC effective, out NotifyList<ExposureMappingSubscription> map_provider )
        {
            throw new NotImplementedException("need to add storage by effective");
            return m_LookupByEffective.TryGetValue(effective, out map_provider); 
        }

        public bool TryGet( ExposureMappingSubscription.UniqueKey key, out ExposureMappingSubscription mapping )
        {
            return m_Mappings.TryGetValue(key, out mapping); 
        }
    }
}
